package com.czmh.etc.interpreter.Utils;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.czmh.etc.interpreter.entity.base.EnMessagetype;
import com.czmh.etc.interpreter.entity.base.InterpreterContent;
import com.czmh.etc.interpreter.entity.request.KeyRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Huangyk on 2019/6/6.
 */
public class CXFUtils {

    public static Object[] Call(String url, String methodName, List<Object> params){
        try {
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(url);

            Object[] objects = client.invoke(methodName, params.toArray());

            return objects;
        }catch (Exception exp){
            exp.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args){
        try {
            KeyRequest request = new KeyRequest();
            request.setMsgLen(103);
            request.setMsgType(EnMessagetype.KeyRequest);
            request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
            request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
            String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
            request.setSendTime(sendTimeStr);
            request.setVerifyCode(new Long("2518611137"));
            request.setWasteSn("80" + sendTimeStr + "0713");
            request.setPosID("8000000");
            request.setTerminalID("  100000000001");
            request.setPosDate(sendTimeStr);

            String jsonStr = "";
            ObjectMapper mapper = new ObjectMapper();
            jsonStr = mapper.writeValueAsString(request);

//            String url = "http://192.168.157.132:9090/mex?wsdl";
            String url = "http://172.21.14.129:9090/mex?wsdl";
            String methodName = "Request";

            List<Object> params = new ArrayList<Object>();
            params.add(EnMessagetype.KeyRequest.getMessageType());
            params.add(jsonStr);

            Object[] result = CXFUtils.Call(url, methodName, params);

            for(Object obj : result){
                System.out.println("------------------------------------------------------------------------------");
                System.out.println("" + obj);
                System.out.println("------------------------------------------------------------------------------");
            }
        }catch (Exception exp){
            exp.printStackTrace();
        }
    }
}
